package com.cdtu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.entity.Certify;
import com.cdtu.entity.RUserSpeciality;
import com.cdtu.entity.Speciality;
import com.cdtu.mapper.CertifyMapper;
import com.cdtu.mapper.RUserSpecialityMapper;
import com.cdtu.mapper.SpecialityTypeMapper;
import com.cdtu.service.SpecialityService;
import com.cdtu.mapper.SpecialityMapper;
import com.cdtu.vo.SpecialityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* @author Sakura
* @description 针对表【speciality】的数据库操作Service实现
* @createDate 2023-01-14 11:12:46
*/
@Service
public class SpecialityServiceImpl extends ServiceImpl<SpecialityMapper, Speciality>
    implements SpecialityService{

    @Autowired
    private SpecialityMapper specialityMapper;

    @Autowired
    private SpecialityTypeMapper specialityTypeMapper;

    @Autowired
    private RUserSpecialityMapper rUserSpecialityMapper;

    @Override
    public List<SpecialityVO> findByIds(List<Long> ids, long uid) {

        //如果ids为0，则返回空的集合
        if (ids.size()==0){
            List<SpecialityVO> result=new ArrayList<>();
            return result;
        }

        List<Speciality> specialities = specialityMapper.selectBatchIds(ids);

        String[] color={"success","info","danger","warning"};

        Random random=new Random();

        //ids不为空则继续向下查询
        List<SpecialityVO> specialityName=new ArrayList<>();

        for (Speciality speciality : specialities) {
            SpecialityVO vo = new SpecialityVO();
            vo.setLabel(speciality.getName());
            vo.setType(color[random.nextInt(4)]);
            Long sid = speciality.getId();

//            根据特长id查询特长详细信息，封装到详细情况
//            String detail = rUserSpecialityMapper.findDetialBySpecialityIdAndUserId(sid,uid).getDetial();
            RUserSpeciality rUserSpeciality = rUserSpecialityMapper.findIdAndDetialByUserIdAndSpecialityId(uid, sid);
            vo.setDetail(rUserSpeciality.getDetial());

            vo.setRus(rUserSpeciality.getId());
            specialityName.add(vo);
        }
        return specialityName;
    }

    @Override
    public SpecialityVO findById(long id) {
        Speciality speciality = specialityMapper.selectById(id);

        return null;
    }

    @Override
    public List<Speciality> findByTypeId(Long typeId) {
        return specialityMapper.findByTypeId(typeId);
    }

    @Override
    public List<SpecialityVO> findByIdsToUpdate(List<Long> ids, long uid) {
        //如果ids为0，则返回空的集合
        if (ids.size()==0){
            List<SpecialityVO> result=new ArrayList<>();
            return result;
        }

        //ids不为空则继续向下查询
        List<Speciality> specialities = specialityMapper.selectBatchIds(ids);

        //要返回的集合
        List<SpecialityVO> specialityName=new ArrayList<>();

        for (Speciality speciality : specialities) {
            Long sid = speciality.getId();
            
            SpecialityVO vo=new SpecialityVO();
            vo.setId(sid);
            vo.setName(speciality.getName());

            //根据特长id查询特长详细信息，封装到详细情况
//            String detail = rUserSpecialityMapper.findDetialBySpecialityIdAndUserId(sid,uid).getDetial();

//            RUserSpeciality rUserSpeciality = rUserSpecialityMapper.findIdAndDetialByUserIdAndSpecialityId(uid, sid);
            LambdaQueryWrapper<RUserSpeciality> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(RUserSpeciality::getUserId,uid);
            queryWrapper.eq(RUserSpeciality::getSpecialityId,sid);
            List<RUserSpeciality> rUserSpecialityList = rUserSpecialityMapper.selectList(queryWrapper);
            RUserSpeciality rUserSpeciality = rUserSpecialityList.get(0);
            vo.setDetail(rUserSpeciality.getDetial());
            vo.setRus(rUserSpeciality.getId());
            Long typeId = specialityMapper.selectById(sid).getTypeId();
            String typeName = specialityTypeMapper.selectById(typeId).getName();
            vo.setType(typeName);

            specialityName.add(vo);
        }

        return specialityName;
    }
}




